//
//  EquipmentViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/11/3.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "EquipmentViewController.h"
#import "BELManagement.h"
#import "BLEHelpViewController.h"

@interface EquipmentViewController ()<BELManagementDelegate>
@property (weak, nonatomic) IBOutlet UILabel *labTip;
@property (weak, nonatomic) IBOutlet UIImageView *imgStart;
@property (weak, nonatomic) IBOutlet UILabel *labStart;
@property (weak, nonatomic) IBOutlet UIButton *btnBLE;
@property (strong, nonatomic) BELManagement *belManagement;

@end

@implementation EquipmentViewController
@synthesize belManagement;

- (void)viewDidLoad {
    //
    [super viewDidLoad];
    self.title = @"蓝牙连接与配对";
    [self showNavgationWithLeftItemWithTitle:@"首页"];
    [self addHelp];
    belManagement = [BELManagement sharedInstance];
    belManagement.delegate = self;
    self.btnBLE.hidden = YES;
//    if (belManagement.bluetoothStatus == BluetoothStatusConnection) {
//        [self backBluetoothStatus];
//    }else{
        [belManagement creatCentralManagement];
//    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 44;
        }else {
            return 156;
        }
    }else {
        if (indexPath.row == 0) {
            return 143;
        }else {
            if (SCREEN_WIDTH == 375) {
                return 235;
            }else if(SCREEN_WIDTH == 414){
                return 273;
            }else{
                if (SCREEN_HEIGHT == 480) {
                    return 130;
                }else{
                    return 130;
                }
            }
        }
    }
}

- (void)addHelp{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"icon_help"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(pushHelp) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.tintColor = [UIColor whiteColor];
    rightBtn.tag = kNavigationBarRightBtnTag;
    CGFloat height = kNavigationBtnHeight;
    rightBtn.frame = CGRectMake(0,
                                kNavigationBtnSpaceHorizontal,
                                height,
                                height);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

- (void)viewDidDisappear:(BOOL)animated{
    [belManagement stopSearch];
}

- (void)backBluetoothStatus{
    if (belManagement.bluetoothStatus == BluetoothStatusPhoneNotOpen) {
        self.btnBLE.hidden = YES;
        self.imgStart.image = [UIImage imageNamed:@"icon_noconnect"];
        self.labStart.text = @"未连接";
        self.labStart.textColor = [UIColor redColor];
        self.labTip.text = @"您的手机蓝牙未打开，如果要使用同步功能，必须先开启手机蓝牙";
        self.labTip.frame = CGRectMake(31, 2, SCREEN_WIDTH - 39, 39);
    }else if (belManagement.bluetoothStatus == BluetoothStatusPhoneOpen || belManagement.bluetoothStatus == BluetoothStatusEquipmentNotOpen){
        self.btnBLE.hidden = YES;
        self.imgStart.image = [UIImage imageNamed:@"icon_notconnected"];
        self.labStart.text = @"未连接";
        self.labStart.textColor = RGB(253, 130, 0);
        self.labTip.text = @"您的手机蓝牙已打开，请确保久乐的蓝牙也打开了。若不清楚怎么操控，请查看帮助说明";
        self.labTip.frame = CGRectMake(31, 2, SCREEN_WIDTH - 39, 39);
        [belManagement searchPeripheral:15];
    }else if (belManagement.bluetoothStatus == BluetoothStatusConnection){
        self.btnBLE.hidden = NO;
        self.imgStart.image = [UIImage imageNamed:@"icon_alreadyconnected"];
        self.labStart.text = @"已连接";
        self.labStart.textColor = RGB(253, 130, 0);
        self.labTip.text = @"您已成功连接久乐终端设备";
        self.labTip.frame = CGRectMake(31, -3, SCREEN_WIDTH - 31, 39);
    }else if(belManagement.bluetoothStatus == BluetoothStatusConnectionTimeOut){
        NSLog(@"蓝牙连接超时");
    }else{
        self.btnBLE.hidden = YES;
        self.imgStart.image = [UIImage imageNamed:@"icon_noconnect"];
        self.labStart.text = @"未连接";
        self.labStart.textColor = [UIColor redColor];
        self.labTip.frame = CGRectMake(31, 2, SCREEN_WIDTH - 39, 39);
    }
}



- (IBAction)offBluetooh:(id)sender {
}

- (void)pushHelp{
    BLEHelpViewController *help = [[BLEHelpViewController alloc] init];
    [self.navigationController pushViewController:help animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
